package cn.zz.veggie.persistence.repository;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.zz.comm.core.types.query.PageQuery;
import cn.zz.comm.exception.classes.FatalErrorException;
import cn.zz.comm.types.query.BaseQuery;
import cn.zz.comm.util.CollectorUtil;
import cn.zz.comm.util.NumberUtil;
import cn.zz.veggie.model.domain.Product;
import cn.zz.veggie.model.query.ProductQuery;
import cn.zz.veggie.persistence.dao.ProductMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 商品信息表(Product)repo
 *
 * @author zhongq
 * @since 2024-04-27 12:30:21
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class ProductRepository {

	private final ProductMapper productMapper;

	public Product findById(Integer id) {
		if (!NumberUtil.isPositive(id)) {
			return null;
		}
		return productMapper.selectById(id);
	}

	public Map<Integer, Product> findByIds(Collection<Integer> ids) {
		if (CollectionUtil.isEmpty(ids)) {
			return Collections.emptyMap();
		}
		final List<Product> records = productMapper.selectBatchIds(ids);
		return CollectorUtil.btmByKeyFun(records, Product::getId);
	}

	public List<Product> findByPage(ProductQuery query, PageQuery page) {
		final LambdaQueryWrapper<Product> queryWrapper = query.toQueryWrapper();
		final Page<Product> queryPage = query.toPage(page);
		List<Product> records = productMapper.selectList(queryPage, queryWrapper);
		page.setTotal((int) queryPage.getTotal());
		return records;
	}

	public void save(Product product) {
		Objects.requireNonNull(product);
		if (product.getId() == null) {
			// insert
			final int effect = productMapper.insert(product);
			if (effect != 1) {
				throw new FatalErrorException("created product fail ");
			}
			log.info("created product for {}", product.getId());
		} else {
			// update
			final int effect = productMapper.updateById(product);
			if (effect != 1) {
				throw new FatalErrorException("update product fail ");
			}
			log.info("updated product for {}", product.getId());
		}
	}

	public void deleteById(Integer id) {
		Objects.requireNonNull(id);
		final int effect = productMapper.deleteById(id);
		if (effect != 1) {
			log.error("delete");
			throw new FatalErrorException("delete product fail ");
		}
		log.info("delete product for {}", id);
	}

	public void deleteByIds(Collection<Integer> ids) {
		if (CollUtil.isEmpty(ids)) {
			return;
		}
		final int effect = productMapper.deleteByIds(ids);
		log.info("delete product for {}", effect);
	}

	public List<Integer> findIdsByName(String productName) {
		if (StrUtil.isBlank(productName)) {
			return null;
		}
		final ProductQuery query = new ProductQuery();
		query.likeName = productName;
		query.column = BaseQuery.DB_PATTERN_ID;
		final List<Product> records = productMapper.selectList(query.toQueryWrapper());
		return CollectorUtil.bwl(records, Product::getId);
	}
}